import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import javax.microedition.io.Connector;
import javax.microedition.io.SocketConnection;
import javax.microedition.io.StreamConnection;

public class J2meSender extends Thread{
  // StreamConnection allows bidirectional communication
  private StreamConnection streamConnection = null;

  // use OutputStream to send requests
  private OutputStream outputStream = null;
  private DataOutputStream dataOutputStream = null;

  // use InputStream to receive responses from Web server
  private InputStream inputStream = null;
  private DataInputStream dataInputStream = null;

  // specify the connect string
  private String connectString; //= "socket://www.java-samples.com:80";
  private String mensaje="@@@";
  private static int refresco=0;
  // use a StrignBuffer to store the retrieved page contents
  //private StringBuffer results;

  public  J2meSender(int port, String ip) {
	  connectString = "socket://" + ip + ":" + port;
	    try {

	    	if ((streamConnection==null)){
	    		String host = System.getProperty("microedition.hostname");
	    		//System.out.println(host);

	    		// establish a socket connection with remote server
	    		//System.out.println("Intentanco conectar con: "+ connectString);
	    	  	streamConnection = (StreamConnection) Connector.open(connectString, Connector.READ_WRITE);
	    	  	// create DataOuputStream on top of the socket connection
	    	   	outputStream = streamConnection.openOutputStream();
	    	 	dataOutputStream = new DataOutputStream(outputStream);
	    	   	// create DataInputStream on top of the socket connection
	    	   	inputStream = streamConnection.openInputStream();
	    	  	dataInputStream = new DataInputStream(inputStream);
	    	}

	    } catch (Exception e) {
	      System.err.println("Exception caught:" + e);
	    }
    // initializing GUI display
    //results = new StringBuffer();
  }
  public  J2meSender(StreamConnection c, int port, String ip) {
	  streamConnection=c;
	    try {

	    	if ((outputStream==null)){

	    	  	// create DataOuputStream on top of the socket connection
	    	   	outputStream = streamConnection.openOutputStream();
	    	 	dataOutputStream = new DataOutputStream(outputStream);
	    	   	// create DataInputStream on top of the socket connection
	    	   	inputStream = streamConnection.openInputStream();
	    	  	dataInputStream = new DataInputStream(inputStream);
	    	}

	    } catch (Exception e) {
	      System.err.println("Exception caught:" + e);
	    }
    // initializing GUI display
    //results = new StringBuffer();
  }

  public void send(String Mensaje) {
		mensaje=Mensaje+"\n";
	    try {

	    	if ((streamConnection==null)){
	    		String host = System.getProperty("microedition.hostname");
	    		//System.out.println(host);

	    		// establish a socket connection with remote server
	    		//System.out.println("Intentanco conectar con: "+ connectString);
	    	  	streamConnection = (StreamConnection) Connector.open(connectString, Connector.READ_WRITE);
	    	  	// create DataOuputStream on top of the socket connection
	    	   	outputStream = streamConnection.openOutputStream();
	    	 	dataOutputStream = new DataOutputStream(outputStream);
	    	   	// create DataInputStream on top of the socket connection
	    	   	inputStream = streamConnection.openInputStream();
	    	  	dataInputStream = new DataInputStream(inputStream);
	    	}
		    if (outputStream!=null){
		    		//System.out.println(17);
			    	String tosend=mensaje;
			    	outputStream.write(tosend.getBytes(), 0, tosend.getBytes().length);
			    	//System.out.println(19);
			    	refresco++;
			    	if (refresco >=1){
				    	outputStream.flush();
				    	refresco=0;
			    	}
			    	//System.out.println(20);
		    }

	    } catch (Exception e) {
	      System.err.println("Exception caught:" + e);
	    }
  }
  public void flush() {
		// TODO Auto-generated method stub
	  try{
		  outputStream.flush();
	  }catch(Exception e){

	  }

	}

public void run() {
	// TODO Auto-generated method stub


}
public void send2(String Mensaje) {
	send(Mensaje);
}
}